package handler

import (
	"github.com/gin-gonic/gin"
	"keep/api/client"
	"keep/api/models"
	keep "keep/common/proto"
	"net/http"
)

func ClassInfo(c *gin.Context) {
	var data models.ClassInfo
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	info, err := client.ClassInfo(c, &keep.KeepReservationInfoReq{
		Name: data.Name,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "团课详情展示成功",
		"data": info.List,
	})
}

func ClassReservation(c *gin.Context) {
	var data models.ClassReservation
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	id := c.GetUint("userId")

	info, err := client.ClassReservation(c, &keep.KeepReservationReq{
		ClassId: data.ClassId,
		Sum:     data.Sum,
		Notes:   data.Notes,
		UserId:  int64(id),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "团课预约成功",
		"data": info.Success,
	})
}

func ReservationConfirm(c *gin.Context) {
	var data models.ClassReservation
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	id := c.GetUint("userId")

	info, err := client.ReservationConfirm(c, &keep.ReservationConfirmReq{
		ClassId: data.ClassId,
		Sum:     data.Sum,
		Notes:   data.Notes,
		UserId:  int64(id),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "团课信息确认成功",
		"data": info.List,
	})
}

func MyReservation(c *gin.Context) {
	var data models.MyReservation
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	info, err := client.MyReservation(c, &keep.MyReservationInfoReq{
		Status: data.Status,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "我的预约查询成功",
		"data": info.List,
	})
}

func RemindUser(c *gin.Context) {
	var data models.Times
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	info, err := client.RemindUser(c, &keep.RemindUserReq{Time: data.Time})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "课程倒计时小于30分钟",
		"data": info.Success,
	})
}

func DelReservation(c *gin.Context) {
	var data models.Timess
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	info, err := client.DelReservation(c, &keep.DelReservationReq{
		Time: data.Time,
		Id:   data.ID,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "取消预约成功",
		"data": info.Success,
	})
}
